package cn.imokay;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.FileInputStream;
import java.io.IOException;

public class WordWaterremark {

    public static XWPFParagraph createWatermarkParagraph(XWPFParagraph paragraphT, String text, String imagePath) throws IOException, InvalidFormatException {
        XWPFParagraph paragraph = paragraphT;
        paragraph.setAlignment(ParagraphAlignment.CENTER);

        // 设置文本水印 
        XWPFRun textRun = paragraph.createRun();
        textRun.setText(text);
        textRun.setFontSize(48);
        textRun.setColor("808080");
        textRun.setFontFamily("Arial");
        textRun.setBold(false);
        textRun.setItalic(true);
        textRun.setUnderline(UnderlinePatterns.NONE);
        textRun.setTextPosition(200);  // 调整文本位置 

        // 设置图片水印 
        XWPFRun imageRun = paragraph.createRun();
        imageRun.addPicture(new  FileInputStream(imagePath), XWPFDocument.PICTURE_TYPE_PNG, imagePath, Units.toEMU(200),  Units.toEMU(200));
        imageRun.setTextPosition(200);  // 调整图片位置 

        // 设置倾斜效果 
        CTPPr ppr = paragraph.getCTP().getPPr();
        if (ppr == null) {
            ppr = paragraph.getCTP().addNewPPr();
        }
        CTShd shd = ppr.addNewShd();
        shd.setVal(STShd.CLEAR);
        shd.setColor("auto");
        shd.setFill("FFFFFF");

        CTRPr rpr = textRun.getCTR().addNewRPr();
        CTTextEffect effect = rpr.addNewEffect();
        effect.setVal(STTextEffect.BLINK_BACKGROUND);

        return paragraph;
    }
} 